package com.bytedance.common.plugin.framework.model;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.c.SharedPreferenceCompat;

import org.json.JSONObject;

public class PluginItem {
    public String packageName;
    public Integer apkVersionCode;

    public PluginItem(String packageName, Integer apkVersionCode) {
        this.packageName = packageName;
        this.apkVersionCode = apkVersionCode;
    }

    public JSONObject toJO() {
        JSONObject jo = new JSONObject();
        try {
            jo.put("package_name", this.packageName);
            jo.put("apk_version_code", this.apkVersionCode);
        } catch (Exception e) {
        }

        return jo;
    }

    public final void saveData(SharedPreferences.Editor editor, boolean commit) {
        if (editor != null && (this.b())) {
            JSONObject v0 = this.toJO();
            if (Logger.debug()) {
                Logger.d("PluginUpdateHelper", "saveData json = " + v0);
            }

            editor.putString(this.getSpKey(), v0.toString());
            if (commit) {
                SharedPreferenceCompat.commit(editor);
            }
        }
    }

    public boolean a(PluginItem pluginItem) {
        return true;
    }

    public boolean b() {
        return (!TextUtils.isEmpty(this.packageName))
                && com.bytedance.common.plugin.a.b(this.packageName)
                && this.apkVersionCode.intValue() > 0;
    }

    protected String getSpKey() {
        return "plugin_" + this.packageName;
    }

    public boolean equals(Object obj) {
        if (!(obj instanceof PluginItem)) {
            return super.equals(obj);
        } else if (this.packageName != null) {
            return this.packageName.equals(((PluginItem) obj).packageName);
        } else if (((PluginItem) obj).packageName == null) {
            return true;
        } else {
            return false;
        }
    }

    public int hashCode() {
        return this.packageName == null ? super.hashCode() : this.packageName.hashCode();
    }
}

